<?php
/**
 * OrderModel.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/14
 * Time: 13:20
 */
namespace app\api\model;

use app\common\Singleton;
use think\Db;

class OrderModel
{
    use Singleton;

    /**
     * 添加|修改
     * @param $flag
     * @param $data
     * @param array $extend
     * @return int|string
     */
    public function save($flag,$data,$extend=[])
    {
        if(empty($flag))
        {
            // 添加
            $data['create_ts'] = time();
            return Db::name('order')
                ->insertGetId($data);
        }
        else
        {
            // 编辑
            $data['update_ts'] = time();
            return Db::name('order')
                ->where('id',$flag)
                ->update($data);
        }
    }

    /**
     * 获取详情
     * @param $where
     * @param array $extend
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    public function get($where,$extend=[])
    {
        $handler = Db::name('order');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->find();
    }

    /**
     * 删除
     * @param $flag
     * @param array $extend
     * @return int
     */
    public function delete($flag,$extend=[])
    {
        return Db::name('order')
            ->where('id',$flag)
            ->delete();
    }

    /**
     * 获取列表
     * @param $where
     * @param array $extend
     * @return \think\Paginator
     */
    public function getList($where,$extend=[])
    {
        $handler = Db::name('order');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->paginate();
    }
    
    /**
     * @param $where
     * @param array $extend
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getAll($where,$extend=[])
    {
        $handler = Db::name('order');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->limit(1000)
            ->select();
    }
    
    /**
     * @param $where
     * @param $data
     * @return int|string
     */
    public function update($where,$data=[])
    {
        if(empty($where))
        {
            return false;
        }
        $data['update_ts'] = time();
        return Db::name('order')
            ->where($where)
            ->update($data);
    }
}